package com.boxing.service;

import com.boxing.beans.*;
import com.boxing.dao.BookDao;
import com.boxing.dao.CarDao;
import com.boxing.dao.UserDao;
import com.boxing.utils.Utils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class CarService {
    private CarDao carDao = new CarDao();
    private UserDao userDao = new UserDao();
    private BookDao bookDao = new BookDao();
    public Result<Object> addBook(Car car) {
        Result<Object> objectResult = new Result<>();
        int i = carDao.addBook(car);
        switch (i){
            case 1:
                objectResult.setCode(200);
                objectResult.setMsg("添加成功");
                break;
            case 2:
                objectResult.setCode(201);
                objectResult.setMsg("添加失败");
                break;
            case 3:
                objectResult.setCode(202);
                objectResult.setMsg("添加失败");
                break;
        }
        return objectResult;
    }

    public Result<CarResult> getCar(HttpServletRequest req) {
        int countPrice = 0;
        long l11 = System.currentTimeMillis();
        String userEmail = Utils.getUserEmailFromSession(req);
        List<CarResult.CarItem> carItems = carDao.getCarAllBooks();
        for(CarResult.CarItem item: carItems){
            countPrice += Integer.parseInt(item.getPrice()) * item.getNum();
            item.setImg(Utils.getServerUrl(req)+item.getImg());
            item.setPrice(Utils.formatPrice(item.getPrice()));
        }

        UserInfo userInfo = userDao.unionEmail(userEmail);
        String userName = userInfo.getUser();
        Result<CarResult> carResultResult = new Result<>();
        carResultResult.setCode(200);
        carResultResult.setMsg("");
        CarResult carResult = new CarResult();
        carResult.setName(userName);

//        ArrayList<Car> cars = new ArrayList<>(carDao.getCar());
//        ArrayList<CarResult.CarItem> carItems = new ArrayList<CarResult.CarItem>();
//        long l11 = System.currentTimeMillis();
//        for(Car item: cars){
//            CarResult.CarItem carItem = new CarResult.CarItem();
//            long l1 = System.currentTimeMillis();
//            // 从商品表中查出所需要的信息
//            Book book = bookDao.searchBookFromId(item.getBook_id()+"");
//            long l2 = System.currentTimeMillis();
//            System.out.println("执行一条语句花费的时间 "+(l2-l1));
//
//            carItem.setBook_id(item.getBook_id());
//            carItem.setEmail(item.getEmail());
//            carItem.setNum(item.getNum());
//
//            carItem.setImg(Utils.getServerUrl(req)+book.getImg());
//            carItem.setPrice(Utils.formatPrice(book.getPrice()));
//            carItem.setBookName(book.getName());
//            carItems.add(carItem);
//            countPrice += Integer.parseInt(book.getPrice()) * item.getNum();
//            long l3 = System.currentTimeMillis();
//            System.out.println("执行一条语句花费的时间22 "+(l3-l2));
//        }
        long l12 = System.currentTimeMillis();
        System.out.println("执行循环语句花费的时间 "+(l12-l11));
        carResult.setCountPrice(Utils.formatPrice(countPrice+""));
        carResult.setList(carItems);
        carResultResult.setData(carResult);
        return carResultResult;
    }
}
